package com.wholesale.beans;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class ShippingMethod {

	private String insure ;
	private List<ShippingArea> shippingAreas ;
	private String shippingCode ;
	private String shippingDesc ;
	private int shippingId ;
	private String shippingName ;
	private String shippingOrder ;
	private boolean enabled ;
	private boolean supportCod ;
	private JSONObject jsonObject;
	private double shippingFee ;
	public String getInsure() {
		return insure;
	}

	public void setInsure(String insure) {
		this.insure = insure;
	}
	public List<ShippingArea> getShippingAreas() {
		return shippingAreas;
	}
	public void setShippingAreas(List<ShippingArea> shippingAreas) {
		this.shippingAreas = shippingAreas;
	}
	public String getShippingCode() {
		return shippingCode;
	}
	public void setShippingCode(String shippingCode) {
		this.shippingCode = shippingCode;
	}
	public String getShippingDesc() {
		return shippingDesc;
	}
	public void setShippingDesc(String shippingDesc) {
		this.shippingDesc = shippingDesc;
	}
	public int getShippingId() {
		return shippingId;
	}
	public void setShippingId(int shippingId) {
		this.shippingId = shippingId;
	}
	public String getShippingName() {
		return shippingName;
	}
	public void setShippingName(String shippingName) {
		this.shippingName = shippingName;
	}
	public String getShippingOrder() {
		return shippingOrder;
	}
	public void setShippingOrder(String shippingOrder) {
		this.shippingOrder = shippingOrder;
	}
	public boolean isEnabled() {
		
		return enabled;
	}
	public void setEnabled(boolean enabled) {
		this.enabled = enabled;
	}
	public boolean isSupportCod() {
		return supportCod;
	}
	public void setSupportCod(boolean supportCod) {
		this.supportCod = supportCod;
	}
	
	public double getShippingFee() {
		return shippingFee;
	}

	public void setShippingFee(double shippingFee) {
		this.shippingFee = shippingFee;
	}

	public String toJsonString(){
		JSONObject jsonObject = new JSONObject();
		try {
			jsonObject.put("shippingId", shippingId);
			jsonObject.put("shippingCode", shippingCode);
			jsonObject.put("shippingName", shippingName);
			jsonObject.put("shippingOrder", shippingOrder);
			jsonObject.put("shippingDesc", shippingDesc);
			jsonObject.put("supportCod", supportCod);
			jsonObject.put("insure", insure);
			jsonObject.put("enabled", enabled);
			if(shippingAreas != null){
				JSONArray areas = new JSONArray();
				int size = shippingAreas.size();
				for(int i=0;i<size;i++){
					areas.put(shippingAreas.get(i).toJSONObject());
				}
				jsonObject.put("shippingAreas", areas);
			}
		} catch (Exception e) {
		}
		return jsonObject.toString();
	}
	
	public void jsonAnalytic(String jsonString){
		
		try {
			jsonObject = new JSONObject(jsonString);
			jsonAnalytic(jsonObject);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void jsonAnalytic(JSONObject jsonObject){
		this.jsonObject = jsonObject;
		
			try {
				shippingId = jsonObject.getInt("shippingId");
			} catch (JSONException e) {
				e.printStackTrace();
			}
			try {
				insure = jsonObject.getString("insure");
			} catch (JSONException e) {
				e.printStackTrace();
			}
			try {
				shippingName = jsonObject.getString("shippingName");
			} catch (JSONException e) {
				e.printStackTrace();
			}
			try {
				shippingCode = jsonObject.getString("shippingCode");
			} catch (JSONException e) {
				e.printStackTrace();
			}
			try {
				shippingDesc = jsonObject.getString("shippingDesc");
			} catch (JSONException e) {
				e.printStackTrace();
			}
			try {
				shippingOrder = jsonObject.getString("shippingOrder");
			} catch (JSONException e) {
				e.printStackTrace();
			}
			try {
				shippingFee = jsonObject.getDouble("shippingFee");
			} catch (JSONException e) {
			}
			try{
	
			JSONArray areas = jsonObject.getJSONArray("shippingAreas");
			
			shippingAreas = new ArrayList<ShippingArea>();
			int size = areas.length();
			for(int i=0;i<size;i++){
				ShippingArea shippingArea = new ShippingArea();
				shippingArea.jsonAnalytic(areas.getJSONObject(i));
				shippingAreas.add(shippingArea);
			}
			}
			catch(JSONException e)
			{
				e.printStackTrace();
			}
			try {
				enabled = jsonObject.getBoolean("enabled");
			} catch (JSONException e) {
				e.printStackTrace();
			}
			try {
				supportCod = jsonObject.getBoolean("supportCod");
			} catch (JSONException e) {
				e.printStackTrace();
			}
		
	}
}
